<?php

/**
 * news actions.
 *
 * @package    sf_sandbox
 * @subpackage news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class newsActions extends autonewsActions
{

    public function executeIndex()
    {
        return $this->forward('news', 'showall');
    }

	/**
	* Deletes image by uname (uname from request)
	*/
	public function executeDeleteimg()
	{
		if( $this->getRequestParameter('uname') )
		{
			Files::deleteByUname($this->getRequestParameter('uname'));
		}
		$this->redirect('news/edit?id=' . $this->getRequestParameter('id'));
	}

    protected function saveNews($news)
    {
        /* @var $news News */
        $news->save();
        $fileName = $this->getRequest()->getFileName('news[photo]');
        $filePath = $this->getRequest()->getFilePath('news[photo]');
        //debugvar($filePath . ' -&gt; ' . sfConfig::get('sf_web_dir') . '/' . sfConfig::get('app_images_dir') . '/' . sfConfig::get('app_gallery_dir'));
        if( !empty($filePath) )
        {
            Files::saveTo(
                $fileName
                , $filePath
                , $news->getId()
                , get_user_id()
                , MyConstants::TYPE_NEWS
                , $news->getCulture()
                , $news->getStatusId()
                , $news->getName()
                , ''
            );
        }
    }
    // for set culture and status_id
    protected function getNewsOrCreate($id = 'id')
    {
    	$news = parent::getNewsOrCreate($id);
        $news->setCulture(sfContext::getInstance()->getUser()->getCulture());
        if( $news->getStatusId() < 1 )
        {
        	$news->setStatusId(MyConstants::STATUS_NEW);
        }
        return $news;
    }


    /**
    * User can preview newsletter. It contains $this->header requested from previous action 'Newsletter' and list of newsleads selected from NewsPeer by Ids also requested from previous action
    */
    public function executePreview()
    {
        $this->header = $this->getRequestParameter('header');
        $this->newsLeads = NewsPeer::retrieveByPks($this->getRequestParameter('newsItems'));
    }

    public function executeActivate()
    {
        $news = NewsPeer::retrieveByPk($this->getRequestParameter('id'));
        $news->setStatusId(MyConstants::STATUS_APPROVED);
        $news->save();
        $this->redirect('news/list/id/' . $this->getRequestParameter('id'));
    }

    public function executeDeactivate()
    {
        $news = NewsPeer::retrieveByPk($this->getRequestParameter('id'));
        $news->setStatusId(MyConstants::STATUS_DELETED);
        $news->save();
        $this->redirect('news/list/id/' . $this->getRequestParameter('id'));
    }

    public function executeShowall()
    {
    	$c = new Criteria();
    	$c->add(TreePeer::OBJ_TYPE, MyConstants::TYPE_LINK_TO);
    	$c->add(TreePeer::LINK_TO, 'news/showall');
    	$tree = TreePeer::doSelectOne($c);
    	if($tree)
    	{
    		$this->getUser()->setAttribute('treeId', $tree->getId());
        	$this->getUser()->setAttribute('tree', $tree);
    	}
		$curDate = date('Y-m-d G:i');
        $c = new Criteria();
        $c->Add(NewsPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
		$c->add(NewsPeer::CREATED_AT, $curDate, Criteria::LESS_THAN);
        $c->addDescendingOrderByColumn(NewsPeer::CREATED_AT);

		$pager = new sfPropelPager('News', News::getNewsOnPage());
		$pager->setCriteria($c);
		$pager->setPage($this->getRequestParameter('page', 1));
		$pager->init();
		$this->pager = $pager;

        //$this->news = NewsPeer::doSelect($c);
    }

    public function executeView()
    {
        $this->newsItem = NewsPeer::retrieveByPk($this->getRequestParameter('id'));
        $this->newsItem->setCulture(get_culture());
        $this->forward404Unless($this->newsItem);
        $this->title = $this->newsItem->getName();
        $c = new Criteria();
        $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_NEWS);
        $c->add(FilesPeer::OBJ_ID, $this->newsItem->getId());
        $this->files = FilesPeer::doSelect($c);
    }

}
